//
//  NewfeatureCollectionViewController.swift
//  Super_WeiBo
//
//  Created by 梁超 on 16/3/22.
//  Copyright © 2016年 lc. All rights reserved.
//

import UIKit

private let reuseIdentifier = "reuseIdentifier"

class NewfeatureCollectionViewController: UICollectionViewController {

    // 页面个数
    private let pageCount = 4
    // 布局对象
    private var layout : UICollectionViewFlowLayout = NewfeatureLayout()
    
    init(){
        super.init(collectionViewLayout : layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 在 viewWillAppear 方法中对控件进行布局
    override func viewWillAppear(animated: Bool) {
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(NewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }

    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewfeatureCell
        // 通过设置 cell 的 imgIndex 属性，激发didSet方法，从而设置图片
        cell.imgIndex = indexPath.item
        if indexPath.item != 3
        {
            cell.startBtn.hidden = true
        }
        return cell
    }
    
    // 完全显示一个cell之后调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        // 拿到当前显示所有cell的indexPath
        let path = collectionView.indexPathsForVisibleItems().last!
        if path.item == pageCount - 1
        {
            // 根绝indexPath取cell
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewfeatureCell
            // 执行动画
            cell.startBtnAnimation()
        }
    }
}

// 加了 private 修饰，但同一个文件当中仍然可以访问
class NewfeatureCell : UICollectionViewCell
{
    // 保存图片索引
    private var imgIndex : Int? {
        didSet{
            imgView.image = UIImage(named: "new_feature_\(imgIndex! + 1)")
        }
    }
    
    // 按钮动画
    func startBtnAnimation()
    {
        startBtn.hidden = false
        // 执行动画
        startBtn.transform = CGAffineTransformMakeScale(0.0, 0.0)
        startBtn.userInteractionEnabled = false
        // UIView动画
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 7
        , options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 清空形变
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
            self.startBtn.userInteractionEnabled = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 初始化UI
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI()
    {
        // 向cell中添加子控件
        contentView.addSubview(imgView)
        contentView.addSubview(startBtn)
        // 布局子控件
        imgView.xmg_Fill(contentView)
        startBtn.xmg_AlignHorizontal(type: XMG_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
    }
    
    // MARK : - 懒加载
    private lazy var imgView = UIImageView()
    private lazy var startBtn : UIButton = {
       let btn = UIButton()
        btn.setImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: #selector(NewfeatureCell.pressStartBtn), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    /**
     点击开始 进入主页
     */
    func pressStartBtn()
    {
        NSNotificationCenter.defaultCenter().postNotificationName(Super_ChaoSwitchRootControllerKey, object: true)
    }
    
}

private class NewfeatureLayout : UICollectionViewFlowLayout
{
    // 准备布局
    override func prepareLayout()
    {
        // 设置layout属性
        itemSize = UIScreen.mainScreen().bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        // 设置 collectionView 的属性
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
    }
}
